﻿// Copyright © 2010 Silvestru Iapascurta.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see license.txt for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Karoro
{
    public class Style
    {
        const string itemBegin = "<!--items begin!-->";
        const string itemEnd = "<!--items end!-->";
        const string categoryBegin = "<!--categories begin!-->";
        const string categoryEnd = "<!--categories end!-->";

        public Style(DirectoryInfo path)
        {
            int i = path.Name.IndexOf(".style");
            Name = path.Name.Remove(i);
            Path = path;
            Template = new FileInfo(System.IO.Path.Combine(Path.FullName, "template.html"));
            Html = (new StreamReader(Template.FullName)).ReadToEnd();

            int indexItemBegin = Html.IndexOf(itemBegin) + itemBegin.Length;
            int indexItemEnd = Html.IndexOf(itemEnd);
            int indexCategoryBegin = Html.IndexOf(categoryBegin) + categoryBegin.Length;
            int indexCategoryEnd = Html.IndexOf(categoryEnd);

            try
            {
                ItemBlock = Html.Substring(indexItemBegin, indexItemEnd - indexItemBegin);
                CategoryBlock = Html.Substring(indexCategoryBegin, indexCategoryEnd - indexCategoryBegin);
            }
            catch (ArgumentOutOfRangeException)
            {
                IsValid = false;
            }

            if (Template.Exists && Template.Length != 0)
            {
                IsValid = true;
            }
            else
            {
                IsValid = false;
                Template = null;
            }
        }

        public bool IsValid { get; private set; }
        public string Name { get; private set; }
        public FileInfo Template { get; private set; }
        public string Html { get; private set; }
        public DirectoryInfo Path { get; private set; }
        public string ItemBlock { get; private set; }
        public string CategoryBlock { get; private set; }
    }
}
